/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gmovil_jpa.controladores;

import gmovil_jpa.controladores.exceptions.NonexistentEntityException;
import gmovil_jpa.entidades.ValorAtributoArticulo;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import gmovil_jpa.entidades.Articulo;
import gmovil_jpa.entidades.Atributo;

/**
 *
 * @author castlerock
 */
public class ValorAtributoArticuloJpaController {

    public ValorAtributoArticuloJpaController() {
        emf = Persistence.createEntityManagerFactory("gmovil_jpaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ValorAtributoArticulo valorAtributoArticulo) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Articulo articulo = valorAtributoArticulo.getArticulo();
            if (articulo != null) {
                articulo = em.getReference(articulo.getClass(), articulo.getIdArticulo());
                valorAtributoArticulo.setArticulo(articulo);
            }
            Atributo atributo = valorAtributoArticulo.getAtributo();
            if (atributo != null) {
                atributo = em.getReference(atributo.getClass(), atributo.getIdAtributo());
                valorAtributoArticulo.setAtributo(atributo);
            }
            em.persist(valorAtributoArticulo);
            if (articulo != null) {
                articulo.getValorAtributoArticuloList().add(valorAtributoArticulo);
                articulo = em.merge(articulo);
            }
            if (atributo != null) {
                atributo.getValorAtributoArticuloList().add(valorAtributoArticulo);
                atributo = em.merge(atributo);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ValorAtributoArticulo valorAtributoArticulo) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ValorAtributoArticulo persistentValorAtributoArticulo = em.find(ValorAtributoArticulo.class, valorAtributoArticulo.getIdValorAtributoArticulo());
            Articulo articuloOld = persistentValorAtributoArticulo.getArticulo();
            Articulo articuloNew = valorAtributoArticulo.getArticulo();
            Atributo atributoOld = persistentValorAtributoArticulo.getAtributo();
            Atributo atributoNew = valorAtributoArticulo.getAtributo();
            if (articuloNew != null) {
                articuloNew = em.getReference(articuloNew.getClass(), articuloNew.getIdArticulo());
                valorAtributoArticulo.setArticulo(articuloNew);
            }
            if (atributoNew != null) {
                atributoNew = em.getReference(atributoNew.getClass(), atributoNew.getIdAtributo());
                valorAtributoArticulo.setAtributo(atributoNew);
            }
            valorAtributoArticulo = em.merge(valorAtributoArticulo);
            if (articuloOld != null && !articuloOld.equals(articuloNew)) {
                articuloOld.getValorAtributoArticuloList().remove(valorAtributoArticulo);
                articuloOld = em.merge(articuloOld);
            }
            if (articuloNew != null && !articuloNew.equals(articuloOld)) {
                articuloNew.getValorAtributoArticuloList().add(valorAtributoArticulo);
                articuloNew = em.merge(articuloNew);
            }
            if (atributoOld != null && !atributoOld.equals(atributoNew)) {
                atributoOld.getValorAtributoArticuloList().remove(valorAtributoArticulo);
                atributoOld = em.merge(atributoOld);
            }
            if (atributoNew != null && !atributoNew.equals(atributoOld)) {
                atributoNew.getValorAtributoArticuloList().add(valorAtributoArticulo);
                atributoNew = em.merge(atributoNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = valorAtributoArticulo.getIdValorAtributoArticulo();
                if (findValorAtributoArticulo(id) == null) {
                    throw new NonexistentEntityException("The valorAtributoArticulo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ValorAtributoArticulo valorAtributoArticulo;
            try {
                valorAtributoArticulo = em.getReference(ValorAtributoArticulo.class, id);
                valorAtributoArticulo.getIdValorAtributoArticulo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The valorAtributoArticulo with id " + id + " no longer exists.", enfe);
            }
            Articulo articulo = valorAtributoArticulo.getArticulo();
            if (articulo != null) {
                articulo.getValorAtributoArticuloList().remove(valorAtributoArticulo);
                articulo = em.merge(articulo);
            }
            Atributo atributo = valorAtributoArticulo.getAtributo();
            if (atributo != null) {
                atributo.getValorAtributoArticuloList().remove(valorAtributoArticulo);
                atributo = em.merge(atributo);
            }
            em.remove(valorAtributoArticulo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<ValorAtributoArticulo> findValorAtributoArticuloEntities() {
        return findValorAtributoArticuloEntities(true, -1, -1);
    }

    public List<ValorAtributoArticulo> findValorAtributoArticuloEntities(int maxResults, int firstResult) {
        return findValorAtributoArticuloEntities(false, maxResults, firstResult);
    }

    private List<ValorAtributoArticulo> findValorAtributoArticuloEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ValorAtributoArticulo.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ValorAtributoArticulo findValorAtributoArticulo(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ValorAtributoArticulo.class, id);
        } finally {
            em.close();
        }
    }

    public int getValorAtributoArticuloCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ValorAtributoArticulo> rt = cq.from(ValorAtributoArticulo.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
